    //
//  RootViewController.m
//  iFlower
//
//  Created by Hoang Thuc on 14/07/2010.
//  Copyright 2010 iNIL. All rights reserved.
//

#import "RootViewController.h"
#import "OccasionView.h"
#import "SpecialOccasionView.h"
#import "RegisterView.h"
#import "LoginView.h"
#import "MyAccountView.h"
#import "InternationalView.h"
#import "iFlowerAppDelegate.h"
#import "Request.h"
#import "RegisterView.h"
#import "HelpView.h"

@implementation RootViewController

@synthesize btnRegister;
@synthesize btnLogin;

-(IBAction) button1Pressed
{
	OccasionView * occasionView = [[OccasionView alloc]initWithNibName:@"OccasionView" bundle:nil];
	[self.navigationController pushViewController:occasionView animated:YES];
	[occasionView release];
}
-(IBAction) button2Pressed
{
	SpecialOccasionView * specialOccasionView = [[SpecialOccasionView alloc]initWithNibName:@"SpecialOccasionView" bundle:nil];
	[self.navigationController pushViewController:specialOccasionView animated:YES];
	[specialOccasionView release];
}
-(IBAction) button3Pressed
{
	InternationalView * inter = [[InternationalView alloc]initWithNibName:@"InternationalView" bundle:nil];
	[self.navigationController pushViewController:inter animated:YES];
	[inter release];
}
-(IBAction) button4Pressed
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Logout" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
		[alert show];
		[alert release];
	}else{
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];
	}
}
-(IBAction) buttonHelpPressed
{
	HelpView * help = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil];
	[self.navigationController pushViewController:help animated:YES];
	[help release];
}
-(IBAction) buttonMenu
{
}
-(IBAction) buttonMyAccount
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}

}
-(IBAction) buttonRegister
{
	RegisterView * registerView = [[RegisterView alloc] initWithNibName:@"RegisterView" bundle:nil];
	[self.navigationController pushViewController:registerView animated:YES];
	[registerView release];
}


- (void) contentLoaded:(NSNotification*) notification
{
	NSLog(@"content loaded");
	NSDictionary* dictionary = [[[Request sharedRequest] entries] objectAtIndex:0];
	NSString* status= [dictionary objectForKey:@"status"];
	NSLog(@"status:%@",status);
	if([status isEqualToString:@"OK"])
	{
		[iFlowerAppDelegate shareDelegate].logined = NO;
		[[iFlowerAppDelegate shareDelegate] setUser:nil];
		self.btnRegister.enabled = YES;
		[self.btnLogin setImage:[UIImage imageNamed:@"homebtn4.png"] forState:UIControlStateNormal];
	}
	[[iFlowerAppDelegate shareDelegate] hideLoadingView];
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) contentFailed:(NSNotification*) notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	NSLog(@"content failed");
	[[iFlowerAppDelegate shareDelegate] hideLoadingView];
}

#pragma mark -
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
	if(buttonIndex!= 0)
	{
		[[iFlowerAppDelegate shareDelegate] showLoadingView];
		
		[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentLoaded:) name: @"ContentLoaded" object: nil];
		[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentFailed:) name: @"ContentFailed" object: nil];
		
		[[Request sharedRequest] logout];		
	}
}
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

#pragma mark -
#pragma mark View lifecycle
- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	if([[iFlowerAppDelegate shareDelegate] logined])
	{
		self.btnRegister.enabled = NO;
		[self.btnLogin setImage:[UIImage imageNamed:@"logoutbtn.png"] forState:UIControlStateNormal];
	}else {
		self.btnRegister.enabled = YES;
		[self.btnLogin setImage:[UIImage imageNamed:@"homebtn4.png"] forState:UIControlStateNormal];
	}

}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//sleep(1);
	[self.navigationController setNavigationBarHidden:YES];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.btnRegister = nil;
	self.btnLogin = nil;
}


- (void)dealloc {
	[btnLogin release];
	[btnRegister release];
    [super dealloc];
}


@end
